/*
 * @Description: 
 * @Author: duan_shuangjun 丶
 * @Date: 2025-08-20 21:05:33
 * @LastEditTime: 2025-09-17 12:20:34
 */
import http from '@/utils/http'
import { offlineApi, offlineApiWithParams } from '@/utils/offlineDecorator'

const api = {
    passportLogin: 'passport/login',
    fetchUserInfo: 'retail.user/info',
    editPassword: 'retail.user/renew',
    fetchTicketDetail: 'deskOrder/ticketDetail',
    preOrderTicket: 'deskOrder/lockAndPrintOrder',
    // 获取用户卡券列表
    userCouponList: 'coupon/userCouponList',
    // 获取门店卡券列表
    shopCouponList: 'coupon/shopCouponList',
    // 绑定卡券到订单
    deskOrderBindCoupon: 'deskOrder/bindCoupon',
    // 获取小票设置详情
    ticketSettingDetail: 'setting/detail',
    // 更新小票设置
    ticketSettingUpdate: 'setting/update',
}

// 开启离线 在这里对离线做处理 其他地方不做
class UserApi {
    @offlineApi('users', 'passport/login')
    async passportLogin(data: { username: string; password: string }) {
        const res = await http.post(api.passportLogin, data)
        if (res?.status === 200) {
            // 在线成功时保存凭据
            await window.api.invoke('users', 'save', {
                username: data.username,
                password: data.password,
                userId: String(res?.data?.userId || '')
            })
        }

        return res
    }

    @offlineApiWithParams('userProfile', 'get', (args) => ({ userId: args[0] }))
    async fetchUserInfo(userId: string) {
        const res = await http.post(api.fetchUserInfo)
        if (res?.status === 200) {
            // 在线成功时保存用户档案
            await window.api.invoke('userProfile', 'save', {
                userId: userId,
                profileData: res.data
            })
        }

        return res
    }
}

const userApi = new UserApi()

export const passportLogin = userApi.passportLogin.bind(userApi)
export const fetchUserInfo = userApi.fetchUserInfo.bind(userApi)

// 静默获取用户信息，不显示loading
export function fetchUserInfoSilent() {
    // 直接使用普通请求，loading控制已在拦截器中被注释掉
    return http.post(api.fetchUserInfo, {})
}

export function editPassword(data: { form: { password: string; password_confirm: string } }) {
    return http.post(api.editPassword, data)
}

export function fetchTicketDetail(data: { order_id: string; type: string }) {
    return http.post(api.fetchTicketDetail, data)
}

// 获取在线员工列表
export function onLineClerkList(data?: any) {
    return http.post('/retail.user/list', data)
}

// 生成预结单【可以确定预结，也可以取消预结】
export function preOrderTicket(data: { record_id: string; print?: number; actionType?: string; isPrint?: number }) {
    return http.post(api.preOrderTicket, data)
}

/**
 * 获取用户卡券列表
 * @param data 请求参数
 * @returns Promise
 */
export function userCouponList(data: any) {
    return http.post(api.userCouponList, data)
}

/**
 * 获取门店卡券列表
 * @param params 请求参数
 * @returns Promise
 */
export function shopCouponList(params?: any) {
    return http.get(api.shopCouponList, { params })
}

/**
 * 绑定卡券到订单
 * @param data 请求参数
 * @returns Promise
 */
export function deskOrderBindCoupon(data: any) {
    return http.post(api.deskOrderBindCoupon, data)
}

/**
 * 获取小票设置详情
 * @param data 请求参数
 * @returns Promise
 */
export function ticketSettingDetail(data: any) {
    return http.post(api.ticketSettingDetail, data)
}

/**
 * 更新小票设置
 * @param data 请求参数
 * @returns Promise
 */
export function ticketSettingUpdate(data: any) {
    return http.post(api.ticketSettingUpdate, data)
}